package com.example.mobifin_busticket.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.mobifin_busticket.AppData;

public class TicketDetailsModel extends PassengerDetails implements Serializable{
	String busType, cancellationPolicy, dateOfIssue, destinationCity,
			destinationCityId, doj, inventoryId, operatorServiceCharge, idType,
			serviceTax, pickUpContactNo, pickUpLocationAddress, pickupLocation,
			pickupLocationId, pickupLocationLandmark, pickupTime, pnr,
			primeDepartureTime, serviceCharge, sourceCity, sourceCityId,
			SeatFare, status, tin, travels, TDseatName;
	boolean TDladiesSeat;
	Boolean partialCancellationAllowed;
	ArrayList<TicketDetailsModel> inventoryList = new ArrayList<TicketDetailsModel>();
	
	public ArrayList<TicketDetailsModel> getinventoryList() {
		return inventoryList;
	}

	public void setinventoryList(ArrayList<TicketDetailsModel> inventoryList) {
		this.inventoryList = inventoryList;
	}
	public String getbusType() {
		return busType;
	}

	public void setbusType(String busType) {
		this.busType = busType;
	}

	public String getpickupLocation() {
		return pickupLocation;
	}

	public void setpickupLocation(String pickupLocation) {
		this.pickupLocation = pickupLocation;
	}

	public String getpickUpLocationAddress() {
		return pickUpLocationAddress;
	}

	public void setpickUpLocationAddress(String pickUpLocationAddress) {
		this.pickUpLocationAddress = pickUpLocationAddress;
	}

	public String getTDseatName() {
		return TDseatName;
	}

	public void setTDseatName(String TDseatName) {
		this.TDseatName = TDseatName;
	}

	public boolean getTDladiesSeat() {
		return TDladiesSeat;
	}

	public void setTDladiesSeat(boolean b) {
		this.TDladiesSeat = b;
	}

	public String getcancellationPolicy() {
		return cancellationPolicy;
	}

	public void setcancellationPolicy(String cancellationPolicy) {
		this.cancellationPolicy = cancellationPolicy;
	}

	public String getdateOfIssue() {
		return dateOfIssue;
	}

	public void setdateOfIssue(String dateOfIssue) {
		this.dateOfIssue = dateOfIssue;
	}

	public String getdestinationCity() {
		return destinationCity;
	}

	public void setdestinationCity(String destinationCity) {
		this.destinationCity = destinationCity;
	}

	public String getdestinationCityId() {
		return destinationCityId;
	}

	public void setdestinationCityId(String destinationCityId) {
		this.destinationCityId = destinationCityId;
	}

	public String getdoj() {
		return doj;
	}

	public void setdoj(String doj) {
		this.doj = doj;
	}

	public String getinventoryId() {
		return inventoryId;
	}

	public void setinventoryId(String inventoryId) {
		this.inventoryId = inventoryId;
	}

	public String getoperatorServiceCharge() {
		return operatorServiceCharge;
	}

	public void setoperatorServiceCharge(String operatorServiceCharge) {
		this.operatorServiceCharge = operatorServiceCharge;
	}

	public String getidType() {
		return idType;
	}

	public void setidType(String idType) {
		this.idType = idType;
	}

	public String getserviceTax() {
		return serviceTax;
	}

	public void setserviceTax(String serviceTax) {
		this.serviceTax = serviceTax;
	}

	public String getpickUpContactNo() {
		return pickUpContactNo;
	}

	public void setpickUpContactNo(String pickUpContactNo) {
		this.pickUpContactNo = pickUpContactNo;
	}

	public String getpickupLocationId() {
		return pickupLocationId;
	}

	public void setpickupLocationId(String pickupLocationId) {
		this.pickupLocationId = pickupLocationId;
	}

	public String getpickupLocationLandmark() {
		return pickupLocationLandmark;
	}

	public void setpickupLocationLandmark(String pickupLocationLandmark) {
		this.pickupLocationLandmark = pickupLocationLandmark;
	}

	public String getpickupTime() {
		return pickupTime;
	}

	public void setpickupTime(String pickupTime) {
		this.pickupTime = pickupTime;
	}

	public String getpnr() {
		return pnr;
	}

	public void setpnr(String pnr) {
		this.pnr = pnr;
	}

	public String getprimeDepartureTime() {
		return primeDepartureTime;
	}

	public void setprimeDepartureTime(String primeDepartureTime) {
		this.primeDepartureTime = primeDepartureTime;
	}

	public String getserviceCharge() {
		return serviceCharge;
	}

	public void setserviceCharge(String serviceCharge) {
		this.serviceCharge = serviceCharge;
	}

	public String getsourceCity() {
		return sourceCity;
	}

	public void setsourceCity(String sourceCity) {
		this.sourceCity = sourceCity;
	}

	public String getsourceCityId() {
		return sourceCityId;
	}

	public void setsourceCityId(String sourceCityId) {
		this.sourceCityId = sourceCityId;
	}

	//
	public String getstatus() {
		return status;
	}

	public void setstatus(String status) {
		this.status = status;
	}

	public String gettin() {
		return tin;
	}

	public void settin(String tin) {
		this.tin = tin;
	}

	public String getSeatFare() {
		return SeatFare;
	}

	public void setSeatFare(String SeatFare) {
		this.SeatFare = SeatFare;
	}

	public String gettravels() {
		return travels;
	}

	public void settravels(String travels) {
		this.travels = travels;
	}

	public Boolean getpartialCancellationAllowed() {
		return partialCancellationAllowed;
	}

	public void setpartialCancellationAllowed(Boolean partialCancellationAllowed) {
		this.partialCancellationAllowed = partialCancellationAllowed;
	}

	public static ArrayList<TicketDetailsModel> parseTicketDetails(
			String response) {
		ArrayList<TicketDetailsModel> ticketDetailsList = new ArrayList<TicketDetailsModel>();

		TicketDetailsModel ticketDetailsModel = new TicketDetailsModel();
		try {
			JSONObject ticketDetailsObj = new JSONObject(response);

			ticketDetailsModel
					.setbusType(ticketDetailsObj.getString("busType"));
			ticketDetailsModel.setcancellationPolicy(ticketDetailsObj
					.getString("cancellationPolicy"));
			ticketDetailsModel.setdateOfIssue(ticketDetailsObj
					.getString("dateOfIssue"));
			ticketDetailsModel.setdestinationCity(ticketDetailsObj
					.getString("destinationCity"));
			ticketDetailsModel.setdestinationCityId(ticketDetailsObj
					.getString("destinationCityId"));
			ticketDetailsModel.setdoj(ticketDetailsObj.getString("doj"));
			ticketDetailsModel.setinventoryId(ticketDetailsObj
					.getString("inventoryId"));
			if (ticketDetailsObj.optJSONArray("inventoryItems") instanceof JSONArray) {
				ArrayList<TicketDetailsModel> inventory=new ArrayList<TicketDetailsModel>();
				AppData.inventoryList = 1;
				JSONArray inventoryArray = ticketDetailsObj
						.getJSONArray("inventoryItems");

				for (int i = 0; i < inventoryArray.length(); i++) {
					JSONObject inventoryObj = inventoryArray.getJSONObject(i);

					ticketDetailsModel.setSeatFare(inventoryObj
							.getString("fare"));
					ticketDetailsModel.setTDladiesSeat(inventoryObj
							.getBoolean("ladiesSeat"));
					ticketDetailsModel.setoperatorServiceCharge(inventoryObj
							.getString("operatorServiceCharge"));

					if (inventoryObj.optJSONArray("passenger") instanceof JSONArray) {
						JSONArray passengerArray = ticketDetailsObj
								.getJSONArray("passenger");

						for (int j = 0; j < passengerArray.length(); j++) {
							JSONObject passengerObj = inventoryArray
									.getJSONObject(j);
							ticketDetailsModel.setage(passengerObj
									.getString("age"));
							ticketDetailsModel.setEmailId(passengerObj
									.getString("email"));
							ticketDetailsModel.setgender(passengerObj
									.getString("gender"));
							ticketDetailsModel.setidType(passengerObj
									.getString("idType"));
							ticketDetailsModel.setphno(passengerObj
									.getString("mobile"));
							ticketDetailsModel.settravellerName(passengerObj
									.getString("name"));
							ticketDetailsModel.setprimary(passengerObj
									.getString("primary"));
							ticketDetailsModel.setTitle(passengerObj
									.getString("title"));
						}
					}
					ticketDetailsModel.setTDseatName(inventoryObj
							.getString("seatName"));
					ticketDetailsModel.setserviceTax(inventoryObj
							.getString("serviceTax"));
					inventory.add(ticketDetailsModel);
					ticketDetailsModel.setinventoryList(inventory);
				}
			} else if (ticketDetailsObj.optJSONObject("inventoryItems") instanceof JSONObject) {
				ArrayList<TicketDetailsModel> inventory=new ArrayList<TicketDetailsModel>();
				AppData.inventoryList = 0;
				JSONObject inventoryObj = ticketDetailsObj
						.getJSONObject("inventoryItems");

				ticketDetailsModel.setSeatFare(inventoryObj.getString("fare"));
				ticketDetailsModel.setTDladiesSeat(inventoryObj
						.getBoolean("ladiesSeat"));
				ticketDetailsModel.setoperatorServiceCharge(inventoryObj
						.getString("operatorServiceCharge"));
				if (inventoryObj.optJSONObject("passenger") instanceof JSONObject) {
					JSONObject passengerObj = inventoryObj
							.getJSONObject("passenger");
					ticketDetailsModel.setage(passengerObj.getString("age"));
					ticketDetailsModel.setEmailId(passengerObj
							.getString("email"));
					ticketDetailsModel.setgender(passengerObj
							.getString("gender"));
					ticketDetailsModel.setidType(passengerObj
							.getString("idType"));
					ticketDetailsModel
							.setphno(passengerObj.getString("mobile"));
					ticketDetailsModel.settravellerName(passengerObj
							.getString("name"));
					ticketDetailsModel.setprimary(passengerObj
							.getString("primary"));
					ticketDetailsModel
							.setTitle(passengerObj.getString("title"));
				}
				ticketDetailsModel.setTDseatName(inventoryObj
						.getString("seatName"));
				ticketDetailsModel.setserviceTax(inventoryObj
						.getString("serviceTax"));
				inventory.add(ticketDetailsModel);
				ticketDetailsModel.setinventoryList(inventory);
			}
			ticketDetailsModel.setpartialCancellationAllowed(ticketDetailsObj
					.getBoolean("partialCancellationAllowed"));
			ticketDetailsModel.setpickUpContactNo(ticketDetailsObj
					.getString("pickUpContactNo"));
			ticketDetailsModel.setpickUpLocationAddress(ticketDetailsObj
					.getString("pickUpLocationAddress"));
			ticketDetailsModel.setpickupLocation(ticketDetailsObj
					.getString("pickupLocation"));
			ticketDetailsModel.setpickupLocationId(ticketDetailsObj
					.getString("pickupLocationId"));
			ticketDetailsModel.setpickupLocationLandmark(ticketDetailsObj
					.getString("pickupLocationLandmark"));
			ticketDetailsModel.setpickupTime(ticketDetailsObj
					.getString("pickupTime"));
			ticketDetailsModel.setpnr(ticketDetailsObj.getString("pnr"));
			ticketDetailsModel.setprimeDepartureTime(ticketDetailsObj
					.getString("primeDepartureTime"));
			ticketDetailsModel.setserviceCharge(ticketDetailsObj
					.getString("serviceCharge"));
			ticketDetailsModel.setsourceCity(ticketDetailsObj
					.getString("sourceCity"));
			ticketDetailsModel.setsourceCityId(ticketDetailsObj
					.getString("sourceCityId"));
			ticketDetailsModel.setstatus(ticketDetailsObj.getString("status"));
			ticketDetailsModel.settin(ticketDetailsObj.getString("tin"));
			ticketDetailsModel
					.settravels(ticketDetailsObj.getString("travels"));
			ticketDetailsList.add(ticketDetailsModel);
			AppData.ticketDetailsList = ticketDetailsList;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return ticketDetailsList;

	}
}
